const connect = require('./base');
function noticeServer() {
    this.queryNotice = (req,callback) => {
        //查询公告列表
        let sql;
		if(req.query.id) {
			sql = 'select * from notice where id=' + req.query.id;
			console.log(req.query.id);
		} else {
			sql = 'select * from notice';
		}
        connect.getConnection((err, connection)=>{
            if(err){
                console.log('连接失败');
                return;
            }else{
                connection.query(sql,function (error, row, field) {
                    if(error){
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.addNotice = (params,callback) => {
        let sql = 'insert into notice set ?',data = {
            title: params.title,
            createDate: String(new Date().getTime()),
            star: params.star !==undefined?params.star:0,
            content: params.content
        };
        connect.getConnection((err,connection) => {
            if(err){
                console.log('连接失败');
                return;
            }else{
                connection.query(sql,data,function (error, row, field) {
                    if(error){
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.delNotice = (params,callback) => {
        let sql = 'delete from notice where id='+params.noticeId;
        connect.getConnection((err,connection) => {
            if(err){
                console.log('连接失败');
                return;
            }else{
                connection.query(sql,function (error, row, field) {
                    if(error){
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.modifyNotice = (params,callback) => {
        let sql = 'update notice set title='+params.title+ ' createDate= '+new Date().getTime()+ ' star= '+params.star+' content= '+params.content+ ' where id='+params.id;
        connect.getConnection((err,connection) => {
            if(err){
                console.log('连接失败');
                return;
            }else{
                connection.query(sql,function (error, row, field) {
                    if(error){
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
};
module.exports=noticeServer;
